<?php    
    $hashid                 = arrGet($_POST,"hashid");
    $template               = arrGet($_POST,"template");
    $trainingProgramHashid  = arrGet($_POST,"trainingProgramHashid");
    $workoutDate            = arrGet($_POST,"workoutDate");
    
    # Hae data
    if (!is_array($template) || empty($template)) {
        $template = API::templateLoad(array(
            "hashid" => $hashid
        ));
    }
?>

<?php if ($hashid) { ?>
    <div class="panel panel-default">
        <div class="panel-body">
            <div class="row">                        
                <div class="col-sm-12">
                    <a class="a-navigation"
                      data-ui-view="addworkout"
                      data-training-program-hashid="<?php echo htmlescape($trainingProgramHashid); ?>"
                      data-templatehashid="<?php echo htmlescape($hashid); ?>"
                      href="#">
                        Start workout
                    </a>
                </div>
            </div>
            
            <div class="row">
                <div class="col-sm-12">
                    <a class="a-navigation"
                      data-ui-view="edittemplate"
                      data-hashid="<?php echo htmlescape($hashid); ?>"
                      href="#">
                        Edit template
                    </a>
                </div>
            </div>
            
            <div class="row">
                <div class="col-sm-12">
                    <a class="a-navigation"
                      data-ui-view="addtemplate"
                      data-hashid="<?php echo htmlescape($hashid); ?>"
                      data-copy="1"
                      href="#">
                        Copy template
                    </a>
                </div>
            </div>
        </div>
    </div>
<?php } ?>

<?php
    # Hae suoritetut harjoitukset tiettynä päivänä
    if ($workoutDate && !empty($template)) {
        $search = API::workoutSearch(array(
            "startdate"         => $workoutDate,
            "templatehashid"    => $hashid
        ));
        
        $list = (array)arrGet($search,"list");
        if (count($list) > 0) {
        ?>
            <div class="panel panel-default">
                <div class="panel-body">
                    <strong>Completed workouts on <?php echo htmlescape($workoutDate); ?></strong><br />
                    <?php
                        foreach ($list as $workout) {
                        ?>
                            <a class="a-subview-dialog"
                              data-ui-view="list"
                              data-ui-subview="info"
                              data-ui-title="<?php echo htmlescape($workout["extratext"]); ?>"
                              data-hashid="<?php echo htmlescape($workout["hashid"]); ?>"
                              href="#">
                                <?php echo htmlescape($workout["extratext"]); ?>
                            </a><br />
                        <?php
                        }
                    ?>
                </div>
            </div>
        <?php
        }
    }
?>

<?php
    $records = (array)arrGet($template,"records");
    if (count($records) > 0) {
        $record = trim(arrGet($records[0],"record",""));
        if ($record) {
    ?>
            <div class="panel panel-default">
                <div class="panel-body">
                    <strong>Notes</strong>
                    <p>
                        <?php echo nl2br(htmlescape($record)); ?>
                    </p>
                </div>
            </div>        
    <?php
        }
    }
?>

<table class="table table-striped">
    <thead>
        <tr>
            <th>
                #
            </th>
            <th>
                Name/desc
            </th>
            <th>
                Special&nbsp;/&nbsp;gear
            </th>            
            <th>
                Reps&nbsp;x&nbsp;weight&nbsp;/&nbsp;rest
            </th>
        </tr>
    </thead>
    
    <tbody>
        <?php
            $i = 1;
            $exercises = (array)arrGet($template,"exercises");
            foreach ($exercises as $exercise) {
            ?>
                <tr>
                    <td>
                        <div>
                            <?php echo $i; ?>
                        </div>
                    </td>
                    <td>
                        <div>
                            <?php echo htmlescape($exercise["extratext"]); ?>
                        </div>
                    </td>
                    <td>
                        <?php                            
                            # Erikoistekniikka
                            echo "<div>".
                                htmlescape(getSpecialExerciseDesc($exercise["special"])).
                                "</div>";
                            
                            # Varusteet
                            foreach (getGearDesc() as $gear => $desc) {
                                if (arrGet($exercise,$gear,0) == 1) {
                                    echo "<div class=\"margin-left\">".
                                        "+&nbsp;".
                                        htmlescape($desc).
                                        "</div>";
                                }
                            }
                        ?>
                    </td>                    
                    <td>
                        <?php
                            $exerciseid = null;
                            $barbellType = null;
                            $sets = (array)arrGet($exercise,"sets");
                            foreach ($sets as $set) {
                                $category           = htmlescape($set["category"]);
                                $exerciseText       = htmlescape($set["exercisetext"]);
                                $musclegroup        = htmlescape($set["musclegroup"]);
                                $useWeight          = $set["weight"];
                                $oneRepMaxP         = $set["onerepmax_percent"];                                
                                $repTypeDesc        = getRepetitionsTypeDesc($set["repetitions_type"]);
                                $barbellTypeDesc    = "";
                                
                                # Harjoitteen nimeä ei vielä tiedetä -> hae
                                if (!$exerciseText) {
                                    $exerciseText = getExerciseText($set["exerciseid"]);
                                }
                                $exerciseText = htmlescape($exerciseText);
                                
                                # Pakota numeeriseksi
                                if (!is_numeric($useWeight)) {
                                    $useWeight = 0;
                                }
                                
                                # Laske paino yhden toiston maksimin prosenttiosuudesta
                                if (is_numeric($oneRepMaxP)) {
                                    $useWeight = $useWeight * ($oneRepMaxP/100);
                                }
                                
                                # Onko levytangotyyppiä asetettu
                                if ($set["barbell_type"]) {
                                    $barbellTypeDesc = "&nbsp;(".
                                        htmlescape(getBarbellTypeDesc($set["barbell_type"])).
                                        ")";
                                }
                                
                                echo(
                                    # Harjoite
                                    (($exerciseid != $set["exerciseid"] || $barbellType != $set["barbell_type"])
                                        ?
                                            "<div>".
                                                $musclegroup."&nbsp;-&nbsp;".$category."<br />".
                                                $exerciseText.$barbellTypeDesc.
                                            "</div>"
                                        : ""
                                    ).
                                    
                                    # Toistot
                                    "<div class=\"margin-left\">".
                                    "*&nbsp;".
                                    htmlescape($set["repetitions"]).
                                    
                                    # Toistovälin loppu
                                    ((is_numeric($set["repetitions_end"]) && $set["repetitions_end"] != $set["repetitions"])
                                        ? "&nbsp;-&nbsp;".$set["repetitions_end"]
                                        : ""
                                    ).
                                    
                                    # Kertaa
                                    "&nbsp;x&nbsp;".
                                    
                                    # Paino
                                    htmlescape(number_format($useWeight,2,".","")).
                                    
                                    # Yksikkö
                                    "&nbsp;".
                                    htmlescape($exercise["unit"]).
                                    
                                    # Kautta lepotauko
                                    "&nbsp;/&nbsp;".
                                    htmlescape(secToStr($set["rest_interval_sec"])).
                                    
                                    # Prosenttiosuus yhden toiston maksimista
                                    ((is_numeric($oneRepMaxP))
                                        ? "&nbsp;(".number_format($oneRepMaxP,2,".","")."&nbsp;%)"
                                        : ""
                                    ).
                                    
                                    # Toistotyyppi
                                    (($set["repetitions_type"] != "normal")
                                        ? "&nbsp;<strong>".htmlescape($repTypeDesc)."</strong>"
                                        : ""
                                    ).
                                    
                                    "</div>"
                                );
                                
                                $exerciseid = $set["exerciseid"];
                                $barbellType = $set["barbell_type"]; 
                            }
                        ?>
                    </td>
                </tr>
            <?php
                $i++;
            }
        ?>
    </tbody>
</table>